var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    longitude:'',
    latitude: '',
    markers: [],
    shopinfo:'',
    shoplist:[],
    nearlong:'',
    nearlati:'',
    showmodel: true,
    scale:12,
    mapheight: '40%',
    washtab: 1,
    washlist: [],
    now_page: 1,
    totalpage:1,
    showmoren:true,
  },

  onLoad: function (options) {
  },
  onShow:function(){
    console.log('car_show');
    this.setData({
      now_page:1
    })
    this.getUserLocation();
  },
  previewImg(e){
    console.log(e)
    let url = e.currentTarget.dataset.url;
    let urls = e.currentTarget.dataset.urls;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls:urls
    })
  },
  getUserLocation: function () {
    let that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //获取定位
        console.log('car_success');
        that.getaddress(res);
      },
      fail: function () {
        console.log('car_fail');
        wx.getSetting({
          success: function (res) {
            console.log(res)
            if (res.authSetting["scope.userLocation"] == false) {
              console.log("判断失败后")
              wx.showModal({
                title: '请授权您的地理位置',
                content: '需要您的地理位置',
                success: function (res) {
                  console.log(res.cancel)
                  wx.openSetting({
                    success(res) {
                      if (res.authSetting["scope.userLocation"] == true) {
                        wx.showToast({
                          title: '授权成功',
                          icon: 'success',
                          duration: 1000
                        })
                      } else {
                        wx.showToast({
                          title: '授权失败',
                          icon: 'none',
                          duration: 1000
                        })
                      }
                    },
                    fail: function (res) {
                      console.log("调起失败")
                    }
                  })
                },
                fail: function (res) {
                  console.log(res.cancel)
                }
              })
            } else {
              //用户已授权，但是获取地理位置失败，提示用户去系统设置中打开定位
              wx.showModal({
                title: '',
                content: '请在手机系统设置中打开定位服务',
                showCancel:false,
                confirmText: '确定',
                success: function (res) {
                }
              })
            }
          },
        })

      }
    })
  },
  // 获取当前位置
  getaddress:function(res){
    var that = this;
    var latitude = res.latitude
    var longitude = res.longitude
    that.setData({
      latitude: latitude,
      longitude: longitude
    })
    wx.setStorageSync('latitude', latitude);
    wx.setStorageSync('longitude', longitude);
    that.getnear(longitude, latitude);
    that.getlist(longitude, latitude);
  },
  // 获取最近商铺
  getnear:function(longitude,latitude){
    var that = this;
    var param ={
      url:"api/shop/shopMap",
      data:{
        type:1,
        longitude:longitude,
        latitude:latitude
      },
      success:function(e){
        var marker = [];
        var datalist = e.data.data.shop_list;
        if(datalist.length == 0){
          that.setData({
            showmodel: false
          })
        }else{

          for (var i = 0; i < datalist.length; i++) {
            var id = datalist[i].id; //名称
            var lat = datalist[i].latitude; //经度
            var lon = datalist[i].longitude;//纬度
            var info = {
              id: 0,
              iconPath: "/images/marker.png",
              latitude: '',
              longitude: '',
            };
            info.id = id,
              info.latitude = lat,
              info.longitude = lon,
              marker.push(info);
          }

          var str = JSON.stringify(marker);
          console.log(marker);
          that.setData({
            markers: marker,
          })

        }
        
      },
      
    };
    app.post(param);
  },

  showwashModal:function(e){
    var shopid = e.markerId;
    console.log(shopid)
    wx.navigateTo({
      url: '/pages/wash/washpay/washpay?id=' + shopid,
    })
    // var that = this;
    // var param={
    //   url:"api/shop/serviceDetail",
    //   data:{
    //     shop_id:shopid,
    //     longitude:that.data.longitude,
    //     latitude:that.data.latitude
    //   },
    //   success:function(e){
    //     console.log(e)
    //     if(e.data.code == 1){
    //       that.setData({
    //          shopinfo:e.data.data 
    //       })
    //     }
        
    //   }
    // }
    // app.post(param);
  },

  showdaohang: function (e) {
    // console.log("第二个e",e)
    var lati = e.currentTarget.dataset.lati;
    var long = e.currentTarget.dataset.long;
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.openLocation({
          latitude: parseFloat(lati),
          longitude: parseFloat(long),
        })
      }
    })
  },
  // saoma
  saoma: function () {
    var _this = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        console.log(res)
        var path = res.path;
        wx.redirectTo({
          url:'/'+path,
        })
      }
    })
  },

  maptop: function () {
    this.setData({
      showmodel: true,
      mapheight: '40%'
    })
  },

  mapbottom: function () {
    this.setData({
      showmodel: false,
      mapheight: '76%'
    })
  },

  washselect: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log("index", index)
    this.setData({
      washtab: index,
      now_page: 1
    })
    this.getlist();
  },

  // 获取列表
  getlist: function () {
    var that = this;
    var param = {
      url: "api/shopr/washshopMap",
      data: {
        now_page: that.data.now_page,
        car_type: that.data.washtab,
        longitude: that.data.longitude,
        latitude: that.data.latitude
      },
      success: function (res) {

        var washlist = res.data.data.shop_list;
        console.log("washlist",washlist);
        var totalpage = res.data.data.total_page;
        var marker = [];
        if (washlist.length == 0) {
          that.setData({
            showmoren: false
          })
        } else {

          for (var i = 0; i < washlist.length; i++) {
            var id = washlist[i].id; //名称
            var lat = washlist[i].latitude; //经度
            var lon = washlist[i].longitude;//纬度
            if (washlist[i].cut > 0 || washlist[i].cut == 0) {
              washlist[i].di = true
            }
            washlist[i].cut = Math.abs(washlist[i].cut).toFixed(2);
            
            var info = {
              id: 0,
              iconPath: "/images/marker.png",
              latitude: '',
              longitude: '',
            };
            info.id = id,
              info.latitude = lat,
              info.longitude = lon,
              marker.push(info);
          }

          var str = JSON.stringify(marker);
          console.log(marker);
          that.setData({
            washlist: washlist,
            totalpage:totalpage
            // markers: marker,
          })

        }
      }
    }
    app.post(param);
  },
  //上拉加载
  loadmore: function (res) {
    console.log("res:到底部了",res.target)
    var that = this;
    var now_page = parseInt(that.data.now_page);
    var totalpage = parseInt(that.data.totalpage);
    console.log(that.data.washlist);
    if (now_page == totalpage) {
      wx.showToast({
        title: '没有更多数据了',
        icon: "none",
        duration: 2000
      })
      return false;
    } else {
      now_page = now_page + 1;
      wx.showLoading({
        title: '玩命加载中',
      })
      var that = this;
      var userid = wx.getStorageSync("userId");
      var param = {
        url: "api/shopr/washshopMap",
        data: {
          now_page: now_page,
          car_type: that.data.washtab,
          longitude: that.data.longitude,
          latitude: that.data.latitude
        },
        success: function (res) {
          var washlist = res.data.data.shop_list;
          for (var i = 0; i < washlist.length; i++) {
            if (washlist[i].cut > 0 || washlist[i].cut == 0) {
              washlist[i].di = true
            }
            washlist[i].cut = Math.abs(washlist[i].cut);
           
          }
          that.setData({
            washlist: that.data.washlist.concat(washlist),
            now_page: now_page
          })
          // 隐藏加载框
          wx.hideLoading();

        },
      };
      app.post(param);
    }

  }
})